package calorie.com.calorie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.Md5Utils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ModifyPhoneContact;
import calorie.com.calorie.response.ResultDTO;
import retrofit2.Response;

public class ActivityModifyPhone extends MvpActivity<ModifyPhoneContact.ModifyPhoneContactPresent> implements ModifyPhoneContact.IModifyPhone {
    @BindView(R.id.modify_phone)
    TextView modify_phone;
    @BindView(R.id.et_login_password)
    EditText et_login_password;
    @BindView(R.id.yzm_one)
    EditText yzm_one;
    boolean notPhone = false;
    @BindView(R.id.tv_get_code)
    TextView tv_get_code;

    @Override
    public void getYZM(Response<ResultDTO> response) {
        if (response.body().getCode().equals("200")) {
            ToastUtils.showLongToast(context, "已发送验证码");
            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tv_get_code, 60000, 1000);
            countDownTimerUtils.start();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getCheckResult(Response<ResultDTO> response) {
        if(response.body().getCode().equals("200")){
            //进入下一个页面
            startActivity(new Intent(context,ActivityModifyPhoneSecond.class));
            this.finish();

        }else if(response.body().getCode().equals("201")){
            ToastUtils.showLongToast(context,"服务器异常");
        }else{
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    public void getYZMSecond(Response<ResultDTO> response) {

    }

    @Override
    public void getModifyResult(Response<ResultDTO> response) {

    }

    @Override
    protected ModifyPhoneContact.ModifyPhoneContactPresent createPresent() {
        return new ModifyPhoneContact.ModifyPhoneContactPresent();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        String phone = SPUtils.getString(Constant.PHONE, "", context);
        if (phone == null || phone.equals("")) {
            ToastUtils.showLongToast(context, "没有绑定手机");
            notPhone = true;
        } else {
            if (phone.length() == 11) {
                modify_phone.setText("*******" + phone.substring(7, 11));
            }

        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_phone_one;
    }

    @OnClick({R.id.tv_get_code,R.id.bt_login_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code://获取验证码
                if (notPhone != true) {
                    mPresent.sendCode(context);
                } else {
                    ToastUtils.showLongToast(context, "没有绑定手机");
                }

                break;
            case R.id.bt_login_submit://验证登录信息
                String yzm = yzm_one.getText().toString().trim();
                String loginPassword = et_login_password.getText().toString().trim();
                if(yzm.equals("")){
                    ToastUtils.showLongToast(context, "请输入验证码");
                    return;
                }else if(loginPassword.equals("")){
                    ToastUtils.showLongToast(context, "请输入登录密码");
                    return;
                }

                mPresent.checkInfo(context,yzm,Md5Utils.stringToMD5(loginPassword));
                break;
        }


    }
}
